package com.appspot.aliga;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appspot.aliga.openligadb.LeagueTableUpdater;
import com.appspot.aliga.openligadb.OpenLigaDBUpdater;
import com.appspot.aliga.openligadb.service.Sportsdata;
import com.appspot.aliga.openligadb.service.SportsdataSoap;
import com.appspot.aliga.persistence.PersistenceService;

public class WorkerServlet extends HttpServlet {

	private static final long serialVersionUID = -7894654343676535433L;
	
	private static final Logger log = Logger.getLogger(WorkerServlet.class.getName());

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {

        String leagueID = req.getParameter("leagueID");
        League league = League.forID(leagueID);
        if (league != null) {
            PersistenceService ps = new PersistenceService();
            LeagueTableUpdater ltu = new LeagueTableUpdater();
            SportsdataSoap service = new Sportsdata().getSportsdataSoap();
            OpenLigaDBUpdater updater = new OpenLigaDBUpdater();
            try {
                updater.updateLeague(ps, ltu, service, league);
                log.fine("updated league " + league);
            } catch (Exception e) {
                log.warning("error while updating league " + league + ". Error: " + e.getMessage());
            }
        }

    }
}
